package com.woniuxy.happylife.suggest.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author Fu
 * @Date 2022-02-16 15:08
 */
@Configuration
public class RabbitmqConfiguration {
    /**
     * 创建交换机
     * @return
     */
    @Bean
    public TopicExchange addSuggestTopic(){
        return new TopicExchange("addSuggestTopic");
    }

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue addSuggestQueue(){
        Map<String, Object> arguments=new HashMap<>();

        arguments.put("x-dead-letter-exchange","deadExchange");
        arguments.put("x-dead-letter-routing-key","deadRouting");
        arguments.put("x-message-ttl",10000);
        return new Queue("addSuggestQueue",true,false,false,arguments);
    }

    /**
     * 将队列绑定到路由交换机
     * @param addSuggestTopic
     * @param addSuggestQueue
     * @return
     */
    @Bean
    public Binding bindingAddSuggest(TopicExchange addSuggestTopic,Queue addSuggestQueue){
        return BindingBuilder.bind(addSuggestQueue).to(addSuggestTopic).with("#.addSuggest.#");

    }

    /**
     * 将消息转换为json
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public Queue queueDead(){
        return new Queue("queueDead");
    }
    @Bean
    public TopicExchange deadExchange(){
        return new TopicExchange("deadExchange");
    }
    @Bean
    public Binding bingDead(TopicExchange deadExchange,Queue queueDead){
        return BindingBuilder.bind(queueDead).to(deadExchange).with("#.deadRouting.#");
    }

}
